var timeMD = 0, timeMS = 0;
var curX, curY, curXSCALE, curYSCALE, mpX, mpY;
var selectedId = -1;
function mouseDown(e) { //
    if (e.button == 0) {
        if (curY > (CANVAS_HEIGHT + (isFullScreen() ? 0 : 100) - 150) && curX < 150) {
            setDxDy(curX * 4500 / 150 - CANVAS_WIDTH / 2 / SCALE, (curY - (CANVAS_HEIGHT + (isFullScreen() ? 0 : 100) - 150)) * 4500 / 150 - CANVAS_HEIGHT / 2 / SCALE);
        }
        if (selectedId != -1) {
            ws.sendMessage("us" + String.fromCharCode(selectedId));
        } else {
            ws.sendMessage("medl" + String.fromCharCode(Math.round(mpX)) + String.fromCharCode(Math.round(mpY)));
        }
    }
    if (e.button == 2) {
        if (selectedId != -1) {
            ws.sendMessage("ua" + String.fromCharCode(selectedId));
        } else {
            ws.sendMessage("medr" + String.fromCharCode(Math.round(mpX)) + String.fromCharCode(Math.round(mpY)));
        }
        return false;
    }
}
function mouseOver(e) {
    mouseUp(e);
}
function mouseOut(e) {
    mouseUp(e);
}
function mouseMove(e) {
    var x;
    var y;
    if (e.pageX != undefined && e.pageY != undefined) {
        x = e.pageX;
        y = e.pageY;
    }
    else {
        x = e.clientX + document.body.scrollLeft +
                document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop +
                document.documentElement.scrollTop;
    }
    x -= $("#gameCanvas").offset().left;
    y -= $("#gameCanvas").offset().top;
    curX = x;
    curY = y;
    mpX = curX / SCALE + dx;
    mpY = curY / SCALE + dy;
    var isAtack = false;
    var isFindSelected = false;
    for (var j = 0; j < objects.heroes.length; j++) {
        if (objects.heroes[j].x - 50 < mpX && objects.heroes[j].x + 50 > mpX && objects.heroes[j].y - 50 < mpY && objects.heroes[j].y + 50 > mpY) {
            selectedId = objects.heroes[j].id;
            isFindSelected = true;
            if (objects.heroes[j].teamId != getMe().teamId) {
                isAtack = true;
            }
            break;
        }
    }
    for (var j = 0; j < objects.towers.length; j++) {
        if (objects.towers[j].x - 50 < mpX && objects.towers[j].x + 50 > mpX && objects.towers[j].y - 50 < mpY && objects.towers[j].y + 50 > mpY) {
            selectedId = objects.towers[j].id;
            isFindSelected = true;
            if (objects.towers[j].teamId != getMe().teamId) {
                isAtack = true;
            }
            break;
        }
    }
    if (isAtack) {
        $("#ultBody").css("cursor", "url(img/resource/cur_atack.png), auto");
        img_cursor = "cur_atack";
    } else {
        $("#ultBody").css("cursor", "url(img/resource/cur.png), auto");
        img_cursor = "cur";
    }
    if (!isFindSelected) {
        selectedId = -1;
    }
}
function mouseUp(e) {
    if (e.button == 0) {
        ws.sendMessage("meul" + String.fromCharCode(Math.round(mpX)) + String.fromCharCode(Math.round(mpY)));
    } else if (e.button == 2) {
        ws.sendMessage("meur" + String.fromCharCode(Math.round(mpX)) + String.fromCharCode(Math.round(mpY)));
        return false;
    } else if (e.button == 1) {

    }
}
function wheel(event) {

}
